<?php
namespace app\admin\model;
use think\Model;
class Count extends Model
{

  /**
   * 文件上传
   */
  protected static function init()
  {
    /**
     * 标题添加图片
     */
    Count::event('before_insert', function ($Count) {
        if($_FILES['pic']['tmp_name']){
          $file = request()->file('pic');
          $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
          if($info){
            $pic = '/' . 'uploads' . '/' .$info->getSaveName();
            $Count['pic']=$pic;
          }
        }
    });

    /**
     * 标题修改图片
     */
    Count::event('before_update', function ($Count) {
      $arts = Count::find($Count->id);
      $thumbpath = $_SERVER['DOCUMENT_ROOT'].$arts['pic'];
      if(file_exists($thumbpath)){
        @unlink($thumbpath);
      }
      if($_FILES['pic']['tmp_name']){
        $file = request()->file('pic');
        $info = $file->move(ROOT_PATH . 'public' . '/' . 'uploads');
        if($info){
          $pic = DS . 'uploads' . '/' .$info->getSaveName();
          $Count['pic']=$pic;
        }
      }
    });

    /**
     * 标题删除图片
     */
    Count::event('before_delete', function ($Count) {
      $arts = Count::find($Count->id);
      $thumbpath = $_SERVER['DOCUMENT_ROOT'].$arts['pic'];
      if(file_exists($thumbpath)){
        @unlink($thumbpath);
      }
    });
  }
  
}
?>